package net.zhengxinyun.performance.service.indicators;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.entity.CountryUnitIndicatorsEntity;
import net.zhengxinyun.performance.entity.CountryUnitScoreEntity;
import net.zhengxinyun.performance.entity.RolePerEntity;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.filter.CountryUnitIndicatorsFilter;
import net.zhengxinyun.performance.filter.CountryUnitScoreFilter;
import net.zhengxinyun.performance.mapper.Indicators.CountryUnitIndicatorsEntityMapper;
import net.zhengxinyun.performance.mapper.login.RolePerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;

/**
 * @Auther: hyp
 * @Date: 2018/10/29 18:11
 */
@Service
public class CountryUnitIndicatorsServiceImpl implements CountryUnitIndicatorsService {

    private CountryUnitIndicatorsEntityMapper countryUnitIndicatorsEntityMapper;


    @Autowired
    private RolePerMapper rolePerMapper;
    @Autowired
    public CountryUnitIndicatorsServiceImpl(CountryUnitIndicatorsEntityMapper countryUnitIndicatorsEntityMapper) {
        this.countryUnitIndicatorsEntityMapper = countryUnitIndicatorsEntityMapper;
    }

    @Override
    @Transactional
    public String delete(CountryUnitIndicatorsEntity countryUnitIndicatorsEntity) {
        int i;
/*        CountryUnitIndicatorsEntity entity = countryUnitIndicatorsEntityMapper.selectByPrimaryKey(countryUnitIndicatorsEntity.getId());
        if ("一级指标".equals(entity.getIndexType())){
            CountryUnitIndicatorsFilter countryUnitIndicatorsFilter = new CountryUnitIndicatorsFilter();
            countryUnitIndicatorsFilter.setSuperiorIndexId(entity.getId().toString());
            List<CountryUnitIndicatorsEntity> list =  countryUnitIndicatorsEntityMapper.query(countryUnitIndicatorsFilter);
            for (CountryUnitIndicatorsEntity filter:list) {
               i =  countryUnitIndicatorsEntityMapper.deleteByPrimaryKey(filter.getId());
                if (i == 0){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return "下属指标删除失败，请联系管理员";
                }
            }
        }*/
        i =  countryUnitIndicatorsEntityMapper.deleteByPrimaryKey(countryUnitIndicatorsEntity.getId());
        if (i == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "删除失败，请联系管理员";
        }
        return "删除成功";
    }

    @Override
    public int update(CountryUnitIndicatorsEntity countryUnitIndicatorsEntity) {
        return countryUnitIndicatorsEntityMapper.updateByPrimaryKeySelective(countryUnitIndicatorsEntity);
    }

    @Override
    public int insert(CountryUnitIndicatorsEntity countryUnitIndicatorsEntity) {
        return countryUnitIndicatorsEntityMapper.insert(countryUnitIndicatorsEntity);
    }

    @Override
    public List<CountryUnitIndicatorsEntity> query(CountryUnitIndicatorsFilter countryUnitIndicatorsFilter, UserEntity userEntity) {

        List<CountryUnitIndicatorsEntity> list  = countryUnitIndicatorsEntityMapper.query(countryUnitIndicatorsFilter);

        if("admin".equals(userEntity.getUserPermissions()))
        {
            return  list;
        }
        String index  = "";
        RolePerEntity role = rolePerMapper.selectByPrimaryKey(userEntity.getRoleid());
        if(ObjectUtils.isNotBlank(role))
        {
            index = role.getDepartIndex();
        }else
        {
            index = userEntity.getDepartIndex();

        }
        HashMap<String,Object> map = Util.getIndexIdMap(index);
        List<CountryUnitIndicatorsEntity> list2  = new ArrayList<>();
        for (CountryUnitIndicatorsEntity item: list )
        {
            String id1 = item.getId().toString();
            Object id2 = map.get(id1);
            if(ObjectUtils.isBlank(id2))
            {
                continue;
            }
            if(id1.equals(id2.toString()))
            {
                list2.add(item);
            }

        }



        return list2;
    }

    @Override
    public List<CountryUnitIndicatorsEntity> query(CountryUnitIndicatorsFilter countryUnitIndicatorsFilter) {
        return countryUnitIndicatorsEntityMapper.query(countryUnitIndicatorsFilter);
    }

    @Override
    public Integer queryForCount(CountryUnitIndicatorsFilter countryUnitIndicatorsFilter) {
        return countryUnitIndicatorsEntityMapper.queryForCount(countryUnitIndicatorsFilter);
    }

    @Override
    public void setIdByName(String name, String dateTime, CountryUnitIndicatorsEntity countryUnitIndicatorsEntity) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("dateTime", dateTime);
        String sysId = countryUnitIndicatorsEntityMapper.getIdByIndexName(map);
        countryUnitIndicatorsEntity.setSuperiorIndexId(sysId);
    }


    public List<CountryUnitIndicatorsEntity> queryCountryUnitScore(CountryUnitScoreFilter countryUnitScoreFilter){
        return countryUnitIndicatorsEntityMapper.queryCountryUnitScore(countryUnitScoreFilter);
    }

    public Integer countCountryUnitScore(CountryUnitScoreFilter countryUnitScoreFilter){
        return countryUnitIndicatorsEntityMapper.countCountryUnitScore(countryUnitScoreFilter);
    }

    /**
     *
     * @param countryUnitIndicatorsFilter
     * @return
     */
    public List<Map<String,Object>> queryScoreList(CountryUnitIndicatorsFilter countryUnitIndicatorsFilter){
        List<CountryUnitIndicatorsEntity>  countryUnitlist = countryUnitIndicatorsEntityMapper.queryScoreList(countryUnitIndicatorsFilter);
        List<Map<String,Object>> listmap = new ArrayList<Map<String,Object>>();
        if(countryUnitlist != null && countryUnitlist.size() > 0){
            for (CountryUnitIndicatorsEntity countryUnitIndicatorsEntity: countryUnitlist) {
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("indexName",countryUnitIndicatorsEntity.getIndexName());
                map.put("leadUnit",countryUnitIndicatorsEntity.getLeadUnit());
                map.put("responsibilityUnit",countryUnitIndicatorsEntity.getResponsibilityUnit());
                map.put("direction",countryUnitIndicatorsEntity.getDirection());
                map.put("standardValue",countryUnitIndicatorsEntity.getStandardValue());
                map.put("score",countryUnitIndicatorsEntity.getScore());
                map.put("indexType",countryUnitIndicatorsEntity.getIndexType());
                map.put("superiorIndexId",countryUnitIndicatorsEntity.getSuperiorIndexId());
                map.put("dateTime",countryUnitIndicatorsEntity.getDateTime());
                map.put("monthTime",countryUnitIndicatorsEntity.getMonthTime());
                List<CountryUnitScoreEntity> list = countryUnitIndicatorsEntity.getList();
                if(list != null && list.size() > 0){
                    for (CountryUnitScoreEntity countryUnitScoreEntity: list) {
                        map.put(countryUnitScoreEntity.getDepartment(),countryUnitScoreEntity.getScore());
                    }
                }
                listmap.add(map);
            }
        }
        return listmap;
    }

    public Integer queryForCountIndex(CountryUnitIndicatorsFilter countryUnitIndicatorsFilter){
        return countryUnitIndicatorsEntityMapper.queryForCountIndex(countryUnitIndicatorsFilter);
    }



}
